package com.wpp.mq.rabbitmqsqmple.workqueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.wpp.mq.rabbitmqsqmple.common.Constant;

/**
 * @description: 工作队列模式
 * 生产者发消息，启动多个消费者实例来消费消息，每个消费者仅消费部分信息，可达到负载均衡的效果。
 *
 * @author: WangPengPeng
 * @create: 2021-11-28 20:23
 **/
public class NewTask {
    private static final String QUEUE_NAME = "";
    private static final String[] works = {"hello.", "hello..", "hello...", "hello....", "hello.....", "hello......", "hello.......", "hello........", "hello.........",
            "hello.........."};

    public static void main(String[] args) {
        // 连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置服务器主机名或IP地址
        factory.setHost(Constant.HOST);
        // 设置Erlang的虚拟主机名称
        factory.setVirtualHost(Constant.VIRTUAL_HOST);
        // 设置用户名
        factory.setUsername(Constant.USER_NAME);
        // 设置密码
        factory.setPassword(Constant.PASSWORD);
        // 设置客户端与服务器的通信端口，默认值为5672
        factory.setPort(Constant.PORT);

        try (Connection conn = factory.newConnection();
             Channel channel = conn.createChannel()) {
            String message = "hello";
            for (int i = 0; i < 20; i++) {
                message = message + i;
                // 将消息路由到taskQueue队列
                channel.basicPublish("", "taskQueue", null, message.getBytes("UTF-8"));
                System.out.println(" [x] Sent '" + message + "'");
                message = "hello";
            }

        } catch (Exception e) {

        }
    }

}
